<template>
  <!-- 一条文章单元格 -->
  <van-cell>
    <!-- 标题区域的插槽 -->
    <template #title>
      <div class="title-box">
        <!-- 标题 -->
        <span>{{obj.title}}</span>
        <!-- 单图 -->
        <img v-if="obj.cover.tyoe === 1" class="thumb" :src="obj.cover.images[0]" />
      </div>
      <!-- 三张图片 -->
      <div class="thumb-box" v-if="obj.cover.type === 3">
          <img class="thumb" v-for="imgURL, index in obj.cover.images" :key="index" :src="imgURL" />
      </div>
    </template>
    <!-- label 区域的插槽 -->
    <template #label>
      <div class="label-box">
        <div>
          <span>{{ obj.aut_name }}</span>
          <span>{{ obj.comm_count }}评论</span>
          <span>{{ formatTime(obj.pubdate) }}</span>
        </div>
        <!-- 反馈按钮 -->
        <van-icon @click="crossFn" name="cross" />
        <!-- 反馈面板 -->
        <van-action-sheet
        v-model="show"
        get-container="body"
        :actions="actions"
        :cancel-text="cancelText"
        @closed="closedFn"
        @cancel="onCancel"
        @select="onSelect" />
      </div>
    </template>
  </van-cell>
</template>

<script>
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime' // 获取指定时间到现在的时间差
import zh from 'dayjs/locale/zh-cn' // 转换成中文
import { firstActions, secondActions } from '@/api/report'
import { articleDislikeAPI, reportArticleAPI } from '@/api'
dayjs.locale(zh)
dayjs.extend(relativeTime)
export default {
  props: {
    obj: Object
  },
  data () {
    return {
      show: false,
      actions: firstActions,
      cancelText: '取消'
    }
  },
  methods: {
    // 指定日期的字符串
    formatTime (dataStr) {
      const str = dayjs(dataStr).fromNow()
      return str
    },
    // 点击选项时触发
    async onSelect (item) {
      // 默认情况下点击选项时不会自动收起
      // 可以通过 close-on-click-action 属性开启自动收起
      if (item.name === '反馈垃圾内容') {
        this.actions = secondActions
        this.cancelText = '返回'
      } else if (item.name === '不感兴趣') {
        // 调用接口发送id，并提示用户
        await articleDislikeAPI(this.obj.art_id)
        this.$notify({ type: 'success', message: '反馈成功' })
        this.show = false
      } else {
        const avgObj = {
          artId: this.obj.art_id,
          type: item.value
        }
        if (item.value === 0) {
          avgObj.remark = '其他问题, 你可以自己写个弹出输入框让用户写'
        }
        await reportArticleAPI(avgObj)
        this.$notify({ type: 'success', message: '举报成功' })
        this.show = false
      }
    },
    // 点击x，打开反馈列表
    crossFn () {
      this.show = true
    },
    // 点击取消
    onCancel () {
      this.show = true
      // 如果点击的是返回，重新赋值
      if (this.cancelText === '返回') {
        this.actions = firstActions
      } else {
        // 点击是取消就关闭
        this.show = false
      }
    },
    // 反馈面板-关闭并做完动画
    closedFn () {
      this.actions = firstActions
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span{
    margin: 0 3px;
    &:first-child{
        margin-left: 0;
    }
}

/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
